package org.gujia.seckill.web;

import org.gujia.seckill.dto.Exposer;
import org.gujia.seckill.dto.SeckillExecution;
import org.gujia.seckill.dto.SeckillResult;
import org.gujia.seckill.entity.Seckill;
import org.gujia.seckill.enums.SeckillStateEnum;
import org.gujia.seckill.exception.RepeatKillException;
import org.gujia.seckill.exception.SeckillCloseException;
import org.gujia.seckill.exception.SeckillException;
import org.gujia.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by GuJia on 2017/2/15.
 */
@Controller
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 列表
     * @param model
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(Model model){

        //list.jsp+mode=ModelAndView
        //获取列表页
        List<Seckill> list = seckillService.getSeckillList();

        model.addAttribute("list",list);

        return "list";
    }

    /**
     * 详情
     * @param seckillId
     * @param model
     * @return
     */
    @RequestMapping(value="/{seckillId}/detail")
    public String detail(@PathVariable("seckillId") Long seckillId, Model model){

        if(seckillId == null){
            return "redirect:/seckill/list";
        }

        Seckill seckill = seckillService.getById(seckillId);

        if(null == seckill){
            return "forward:/seckill/list";
        }

        model.addAttribute("seckill",seckill);

        return "detail";
    }

    /**
     * ajax   json  暴露秒杀接口的方法
     * @param seckillId
     * @return
     */
    @RequestMapping(value ="/seckill/{seckillId}/exposer",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SeckillResult<Exposer> exposer(Long seckillId){

        SeckillResult<Exposer> result;
        try {
            Exposer exposer = seckillService.exportSeckillUrl(seckillId);
            result = new SeckillResult<Exposer>(true, exposer);
        }catch (SeckillException e){
            e.printStackTrace();
            result = new SeckillResult<Exposer>(false,e.getMessage());
        }

        return result;
    }
    @RequestMapping(value = "/seckill/{seckillId}/{md5}/execution",
            method=RequestMethod.POST,
            produces = {"application/json;carset=UTF-8"})
    @ResponseBody
    public  SeckillResult<SeckillExecution> execute(@PathVariable("seckillId") Long seckillId,
                                                    @CookieValue(value = "killPhone",required = false) Long userPhone,
                                                    @PathVariable("md5") String md5){

        if(userPhone == null){
            return new SeckillResult<SeckillExecution>(false,"未注册");
        }

        SeckillResult<SeckillExecution> result;

        try {
            SeckillExecution seckillExecution = seckillService.executeSeckillProcedure(seckillId,userPhone,md5);
            result = new SeckillResult<SeckillExecution>(true,seckillExecution);
        }catch (RepeatKillException e){
            SeckillExecution seckillExecution = new SeckillExecution(seckillId, SeckillStateEnum.REPEAT_KILL);
            result = new SeckillResult<SeckillExecution>(false,seckillExecution);
        }catch (SeckillCloseException e){
            SeckillExecution seckillExecution = new SeckillExecution(seckillId, SeckillStateEnum.END);
            result = new SeckillResult<SeckillExecution>(false,seckillExecution);
        }catch (Exception e){
            SeckillExecution seckillExecution = new SeckillExecution(seckillId, SeckillStateEnum.INNER_ERROR);
            result = new SeckillResult<SeckillExecution>(false,seckillExecution);
        }

        return result;
    }

    @RequestMapping(value = "/seckill/time/now",method = RequestMethod.GET)
    @ResponseBody
    public SeckillResult<Long> time(){
        Date now = new Date();
        return new SeckillResult<Long>(true,now.getTime());
    }

}
